package com.zhuzi.guicollect.jsl;

import com.zhuzi.guicollect.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.time.LocalDateTime;

/**
 * @Author: MI
 * @Date: 2021/10/24/20:52
 * @Description:
 */
public class JslQuestion {
    /**
     * 地址
     */
    private String url;
    /**
     * 回复数量
     */
    private int numOfReply;

    /**
     * 标题
     */
    private String title;

    /**
     * 发布时间
     */
    private LocalDateTime publishTime;
    /**
     * 最后修改时间
     */
    private LocalDateTime modifiedTime;

    public static JslQuestion parse(Element ele) {
        Elements select = ele.select(".aw-mod-head");
        String title = select.get(0).getElementsByTag("h1").get(0).text();
        //java 文件名替换
        title = title.replaceAll("[/\\\\:*?|]", "_");
        title = title.replaceAll("[\"<>]", "'");
        String replayOriginal = select.get(1).getElementsByTag("h2").text();
        String digits = StringUtils.getDigits(replayOriginal);
        int replyNum = 0;
        if (StringUtils.isNotBlank(digits)) {
            replyNum = Integer.parseInt(digits);
        }

        String text = ele.select(".aw-question-detail-meta div>span").text();
        String[] dateSplit = StringUtils.split(text, "时间|最后修改|发表");
        LocalDateTime publishDate = DateUtils.parseJslQuesPublish(dateSplit[0].trim());

        JslQuestion jslQuestion = new JslQuestion();
        jslQuestion.setTitle(title);
        jslQuestion.setNumOfReply(replyNum);
        jslQuestion.setPublishTime(publishDate);
        if (dateSplit.length > 1) {
            LocalDateTime modifyDate = DateUtils.parseJslQuesPublish(dateSplit[1].trim());
            jslQuestion.setModifiedTime(modifyDate);
        }
        return jslQuestion;
    }

    @Override
    public String toString() {
        return "JslQuestion{" +
                "url='" + url + '\'' +
                ", numOfReply=" + numOfReply +
                ", title='" + title + '\'' +
                ", publishTime=" + publishTime +
                ", modifiedTime=" + modifiedTime +
                '}';
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getNumOfReply() {
        return numOfReply;
    }

    public void setNumOfReply(int numOfReply) {
        this.numOfReply = numOfReply;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public LocalDateTime getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(LocalDateTime publishTime) {
        this.publishTime = publishTime;
    }

    public LocalDateTime getModifiedTime() {
        return modifiedTime;
    }

    public void setModifiedTime(LocalDateTime modifiedTime) {
        this.modifiedTime = modifiedTime;
    }
}
